//
//  UITextField+BZTheme.m
//  MZBasic
//
//  Created by Sings on 2022/9/7.
//

#import "UITextField+BZTheme.h"

@implementation UITextField (BZTheme)

+ (void)load {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        Class class           = [UITextField class];
        Method originalmethod = class_getInstanceMethod(class, @selector(setPlaceholder:));
        Method swizzledMethod = class_getInstanceMethod(class, @selector(switchSetPlaceholder:));
        method_exchangeImplementations(originalmethod, swizzledMethod);
    });
}

- (void)awakeFromNib{
    [super awakeFromNib];
    NSString *temPlaceholder = self.placeholder ;
    if (temPlaceholder != nil) {
        self.attributedPlaceholder = [[NSAttributedString alloc] initWithString:temPlaceholder attributes:@{NSForegroundColorAttributeName:BZTHEME_COLOR(skin_textfield_placeholder_color)}];
    }
    self.textColor = BZTHEME_COLOR(skin_content_text_level1_color);
}

- (void) switchSetPlaceholder:(NSString *) placeholder {
    if (placeholder == nil || [placeholder isEqualToString:@""]) {
        [self switchSetPlaceholder:placeholder];
        // 覆盖当前对象Block textField外部尽量不要用block 特别是设置placeholder的时候可能会覆盖掉外部block
        BZThemeBlock(self, ^{
        });
        return;
    }
    __weak typeof(self) weakSelf = self;
    BZThemeBlock(self, ^{
        weakSelf.attributedPlaceholder = [[NSAttributedString alloc] initWithString:placeholder attributes:@{NSForegroundColorAttributeName:BZTHEME_COLOR(skin_textfield_placeholder_color)}];
    });
}

@end
